package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.educational.entity.ExternalMeasureScore;
import com.yuyou.fn.educational.mapper.ExternalMeasureScoreMapper;
import com.yuyou.fn.educational.service.IExternalMeasureScoreService;
import com.yuyou.fn.educational.vo.ExternalMeasureScoreQueryVo;
import org.springframework.stereotype.Service;

/**
 *
 * ExternalMeasureScore 表数据服务层接口实现类
 *
 */
@Service
public class ExternalMeasureScoreServiceImpl extends SuperServiceImpl<ExternalMeasureScoreMapper, ExternalMeasureScore,Long> implements IExternalMeasureScoreService {


    @Override
    public boolean checkPass(Long studentId, Long gradeId, Long subjectId, Integer externalScoreType, Double externalPassingScore, boolean isOnline) {

        if(!isOnline ){
            return true ;
        }

        Example<ExternalMeasureScore> measureScoreExample = ExternalMeasureScore.createExample();
        Example.Criteria criteria=measureScoreExample.createCriteria();
        criteria.andEqualTo("studentId",studentId);
        criteria.andEqualTo("gradeId",gradeId);
        criteria.andEqualTo("subjectId",subjectId);
        criteria.andEqualTo("validity",1);
        criteria.andEqualTo("scoreType", externalScoreType );
        if(externalPassingScore == 2 ){
            measureScoreExample.orderBy("score").asc();
        }else{
            measureScoreExample.orderBy("score").desc();
        }

        ExternalMeasureScore externalMeasureScore = findOneByExample(measureScoreExample);

        if(externalMeasureScore == null ){
            return false ;
        }

        if(externalScoreType == 2 ){
            return (int)(externalMeasureScore.getScore() * 10 ) <= (int)(externalPassingScore * 10) ;
        }else{
            return (int)(externalMeasureScore.getScore() * 10 ) >= (int)(externalPassingScore * 10) ;
        }
    }

    @Override
    public Page<ExternalMeasureScore> findByQuery(ExternalMeasureScoreQueryVo externalMeasureScoreQueryVo, int pageNo, int pageSize) {
        Example<ExternalMeasureScore> example = ExternalMeasureScore.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,externalMeasureScoreQueryVo);
        return findPageByExample(example,pageNo,pageSize );
    }
}